#-*- coding: utf-8 -*-
'''
Created on Aug 24, 2012

@author: bbg
'''
from sqlalchemy import Integer, Column, MetaData, Table, String, DateTime
from sqlalchemy.orm import mapper

class LoginSession(object):
    '''
    用户权限信息
    '''
    
    _Mapper_ = None
    _company_id_ = -1
    
    def __init__(self):
        self.user_name = 0
        self.token = ''
        self.login_time = ''        
    
    @staticmethod
    def doMapping(company_id):
        '''
        按company_id分库，每次生成对象前都要执行一次，以便map到正确的数据库上去
        '''
        if LoginSession._company_id_ == company_id:
            return
                
        if LoginSession._Mapper_ is not None:
            LoginSession._Mapper_.dispose()
            
        metaData = MetaData()
        tableDefine = Table('login_session', metaData,
                           Column('id', Integer, primary_key = True),
                           Column('user_name', String),
                           Column('token', String), 
                           Column('login_time', DateTime), schema = "company_%d" % int(company_id))
                
        LoginSession._Mapper_ = mapper(LoginSession, tableDefine)
        
